package com.chenglai.seckill.controller;

import com.chenglai.commonutils.common.Result;
import com.chenglai.commonutils.common.StatusCode;
import com.chenglai.seckill.pojo.GroupOnInfo;
import com.chenglai.seckill.service.GroupOnInfoService;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 类描述：
 *
 * @Author rxh
 * @Date 2023/11/27 13:45
 */
@RestController
@RequestMapping("/groupOn")
public class GroupOnController {

    @Resource
    private GroupOnInfoService groupOnInfoService;

    @PostMapping("/list")
    @ApiOperation(value = "获取团购信息")
    public Result getGroupOnList(@ApiParam(name = "shopId", value = "门店Id") @RequestParam("shopId") Integer shopId,
                                 @ApiParam(name = "userId", value = "用户Id") @RequestParam("userId") Integer userId) {

        try {
            List<GroupOnInfo> list = groupOnInfoService.getGroupOnInfoList(shopId, userId);
            return new Result(true, StatusCode.OK, "查询成功", list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR, "查询失败");
        }
    }

    @PostMapping("/edit")
    @ApiOperation(value = "添加/修改团购信息")
    public Result editGroupOnInfo(GroupOnInfo groupOnInfo) {
        try {
            groupOnInfoService.editGroupOnInfo(groupOnInfo);
            return new Result(true, StatusCode.OK, "操作成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR, "操作失败");
        }
    }

    @GetMapping("/detail")
    @ApiOperation(value = "团购详请")
    public Result detail(@ApiParam(name = "groupId", value = "团购主键") @RequestParam("groupId") Integer groupId) {
        try {
            GroupOnInfo groupOnInfo = groupOnInfoService.detail(groupId);
            return new Result(true, StatusCode.OK, "查询成功", groupOnInfo);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR, "查询失败");
        }
    }

    //处理支付完成之后的成团业务逻辑
    @GetMapping("/groupOnLogic")
    @ApiOperation(value = "成团业务逻辑")
    public void groupOnLogic(@ApiParam(name = "map", value = "参数") @RequestParam("map") Map<String, Object> map) {
        groupOnInfoService.groupOnLogic(map);
    }


    @GetMapping("/groupOnEnd")
    @ApiOperation(value = "团购时间结束，团购作废")
    public void groupOnEnd(@ApiParam(name = "groupId", value = "团购主键") @RequestParam("groupId") Integer groupId){
        groupOnInfoService.groupOnEnd(groupId);
    }

}
